<%-- 
    Document   : saveStop
    Created on : Jul 28, 2009, 4:20:13 PM
    Author     : david
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" 
        import="java.text.DateFormat,
        java.text.SimpleDateFormat,
        java.util.Date,
        java.util.TimeZone,
        com.appspot.nnaplanner.DAL.*,
        com.appspot.nnaplanner.*,
        java.util.logging.Logger
        "%>
<%
            if (request.getUserPrincipal() == null) {
                response.sendError(response.SC_UNAUTHORIZED);
            }

            // try {
            Logger log = Logger.getLogger(this.getServletInfo());

            DALConnection dc = new DALConnection();

            if (request.getParameter("update") == null) {
                DALStop ds = new DALStop();
                String did = request.getParameter("did");
                String sid = request.getParameter("sid");
                DALStop ds1 = DAL.getByID(ds, did);
                DALStop ds2 = DAL.getByID(ds, sid);

                ds1.getByID(did);
                ds2.getByID(sid);
                dc.setDestination(request.getParameter("did"));
                dc.setDestinationName(request.getParameter("dname"));
                dc.setStop(request.getParameter("sid"));
                dc.setStopName(request.getParameter("sname"));
                out.print("{success: true}");
                dc.setKeywords(Utils.getKeywords(dc.getStopName()));
                dc.getKeywords().add(dc.getID());
                dc.getKeywords().add(dc.getStopID());
                dc.getKeywords().add(dc.getDestinationID());
                dc.setID(request.getParameter("did") + request.getParameter("sid"));
                dc.save();
                log.info("id " + ds2.getID() + " param: " + request.getParameter("did"));
                log.info("id " + ds1.getID() + " param: " + request.getParameter("sid"));
                ds1.setConnected(true);

                ds1.save();
                ds2.setConnected(true);
                ds2.save();
            } else {
                dc = dc.getByID(request.getParameter("update"));
                DateFormat df = new SimpleDateFormat("HH:mm");
                Date d = df.parse(request.getParameter("time"));
                Integer i = Integer.parseInt(request.getParameter("duration"));
                dc.addDeparture(d, i, request.getParameter("desc"));
                dc.save();
                NNappData.getGraph().addEdge(dc.getStopID(), dc.getDestinationID());
                NNappData.getGraph().setEdgeWeight(NNappData.getGraph().getEdge(dc.getStopID(), dc.getDestinationID()), i);
                out.print("{ \"success\": \"add times total " + dc.getTimes().size() + "\"}");
            }
            // } catch (Exception ex) {
            //     out.print("{ \"msg\": \"FAILED, params: <br>" + request.getParameter("did") + " " + request.getParameter("dname") + " <br>" +  request.getParameter("sid") +  " "  + request.getParameter("sname") + "\"}");
            // }
%>